package com.lzb.compressmaster.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.SeekBar;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.lzb.compressmaster.BaseAc;
import com.lzb.compressmaster.Content;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.databinding.ActivityPictureSizeCompressBinding;
import com.lzb.compressmaster.utils.CompressConfigBuilder;
import com.lzb.compressmaster.utils.ImageCompressor;
import com.sztle.admodule.apiprocessor.OutRewardAdApi;
import com.sztle.admodule.apiprocessor.listener.IOutRewardCallback;

import java.util.ArrayList;
import java.util.List;

public class PictureSizeCompressActivity extends BaseAc<ActivityPictureSizeCompressBinding> implements View.OnClickListener {

    public static String sPath;
    private ImageCompressor.CompressConfig compressConfig;
    private ImageCompressor.ImageInfo imageInfo;
    private float percent;

    @Override
    protected int onCreate() {
        return R.layout.activity_picture_size_compress;
    }

    @Override
    protected void initView() {
        mDataBinding.ivPictureSizeCompressBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.tvRatioCompress.setOnClickListener(this);
        mDataBinding.tvResolutionCompress.setOnClickListener(this);
        mDataBinding.tvStartCompress.setOnClickListener(this);

        mDataBinding.sbProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mDataBinding.tvSizeProgress.setText(progress + getString(R.string.unit_percent));
                percent = progress * 1f / 100;
                compressConfig.setScalePercent(progress);
                setRatioData();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.tvRatioCompress) {//等比压缩
            clearSelection();
            mDataBinding.tvRatioCompress.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvRatioCompress.setBackgroundResource(R.drawable.compress_mode_sel);
            mDataBinding.llOutputSize.setVisibility(View.VISIBLE);
            compressConfig.setCompressType(ImageCompressor.CompressType.ADVANCED);
            setRatioData();
        } else if (id == R.id.tvResolutionCompress) {//指定宽高
            clearSelection();
            mDataBinding.tvResolutionCompress.setTextColor(Color.parseColor("#FFFFFF"));
            mDataBinding.tvResolutionCompress.setBackgroundResource(R.drawable.compress_mode_sel);
            mDataBinding.llSpecifiedPixel.setVisibility(View.VISIBLE);
            compressConfig.setCompressType(ImageCompressor.CompressType.PIXEL);
            setResolutionData();
        } else if (id == R.id.tvStartCompress) {//开始压缩
            String width = mDataBinding.etSpecifiedWidth.getText().toString();
            String height = mDataBinding.etSpecifiedHeight.getText().toString();
            if (TextUtils.isEmpty(width)) {
                ToastUtils.showShort(getString(R.string.please_input_width));
                return;
            }
            if (TextUtils.isEmpty(height)) {
                ToastUtils.showShort(getString(R.string.please_input_height));
                return;
            }
            compressConfig.setWidth(Integer.parseInt(width));
            compressConfig.setHeight(Integer.parseInt(height));
            OutRewardAdApi.showRewardAd(this, "picture_size_compress", new IOutRewardCallback() {
                @Override
                public void onRewardEnd() {
                    startCompress();
                }
            });
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(sPath).into(mDataBinding.ivImg);

        compressConfig = CompressConfigBuilder.advanced()
                .setFormat(ImageCompressor.OutputFormat.ORIGINAL)
                .setQuality(2)
                .setSuffix("")
                .build();

        imageInfo = ImageCompressor.getImageInfo(sPath);

        mDataBinding.tvBeforeCompress.setText(getString(R.string.before_compress_text) + imageInfo.getWidth() + "x" + imageInfo.getHeight());
        mDataBinding.tvAfterCompress.setText(getString(R.string.after_compress_text) + imageInfo.getWidth() + "x" + imageInfo.getHeight());
        mDataBinding.etSpecifiedWidth.setText(compressConfig.getWidth() + "");
        mDataBinding.etSpecifiedHeight.setText(compressConfig.getHeight() + "");
        mDataBinding.sbProgress.setProgress((int) compressConfig.getScalePercent());
        mDataBinding.tvSizeProgress.setText((int) compressConfig.getScalePercent() + getString(R.string.unit_percent));

        mDataBinding.etSpecifiedWidth.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (!TextUtils.isEmpty(str)) {
                    setResolutionData();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        mDataBinding.etSpecifiedHeight.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String str = charSequence.toString();
                if (!TextUtils.isEmpty(str)) {
                    setResolutionData();
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private void startCompress() {
        List<String> list = new ArrayList<>();
        list.add(sPath);
        ImageCompressor.compressImages(mContext, list, compressConfig, new ImageCompressor.CompressCallback() {
            @Override
            public void onProgress(int current, int total, String fileName) {
                showDialog(getString(R.string.compress_ing));
            }

            @Override
            public void onComplete(List<ImageCompressor.CompressResult> results) {
                dismissDialog();
                PictureCompressSucActivity.listResult = results;
                startActivity(PictureCompressSucActivity.class);
            }

            @Override
            public void onError(String error) {
                dismissDialog();
                ToastUtils.showShort(error);
            }
        });
    }

    private void setRatioData() {
        int width = (int) (imageInfo.getWidth() * percent);
        int height = (int) (imageInfo.getHeight() * percent);
        mDataBinding.tvAfterCompress.setText(getString(R.string.after_compress_text) + width + "x" + height);
    }

    private void setResolutionData() {
        int width = Integer.parseInt(mDataBinding.etSpecifiedWidth.getText().toString());
        int height = Integer.parseInt(mDataBinding.etSpecifiedHeight.getText().toString());
        mDataBinding.tvAfterCompress.setText(getString(R.string.after_compress_text) + width + "x" + height);
    }

    private void clearSelection() {
        mDataBinding.tvRatioCompress.setTextColor(Color.parseColor("#333333"));
        mDataBinding.tvRatioCompress.setBackgroundResource(R.drawable.compress_mode_bg);
        mDataBinding.llOutputSize.setVisibility(View.GONE);
        mDataBinding.tvResolutionCompress.setTextColor(Color.parseColor("#333333"));
        mDataBinding.tvResolutionCompress.setBackgroundResource(R.drawable.compress_mode_bg);
        mDataBinding.llSpecifiedPixel.setVisibility(View.GONE);
    }
}